<script>
	/**
	 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
	 */
	import store from "@/store"
	import {
		mapGetters,
		mapMutations
	} from "vuex"
	import {
		getArchivesListByEnvironment,
		getModulesListToBasicData,
		getDictData,
		getAllUserList,
		getTempleteList
	} from '@/common/environmentalApi/index.js'
	import {
		getListFromNetwork
	} from '@/common/environmentalApi/network.js'
	import {
		getBasicInfoFromLocalData
	} from '@/static/common/js/padLocalFileHandler.js'
	export default {
		onLaunch() {

			// 国际化，设置当前语言
			if (this.vuex_locale) {
				this.$i18n.locale = this.vuex_locale;
				this.$u.api.lang({
					lang: this.vuex_locale
				});
			}
			uni.hideTabBar()

			// #ifdef APP-PLUS
			if (process.env.NODE_ENV !== 'development') {
				if (this.isNetworkConnected && this.isNetworkConnected != null) {
					this.autoLogin()
					this.plusReady()
				}
			}
			// this.initDatebase()
			let that = this
			uni.getNetworkType({
				success: function(res) {
					// res.networkType 的值可以是："wifi"、"2g"、"3g"、"4g" 或 "none"
					if (res.networkType === 'none') {
						console.log('设备处于离线状态');
						store.commit('SET_IS_CONNECTED', {
							isConnected: false
						})
						// #endif
					} else {
						console.log('设备处于联网状态');
						store.commit('SET_IS_CONNECTED', {
							isConnected: true
						})
					}
				}
			});
			// #endif
		},
		computed: {
			...mapGetters(['isNetworkConnected'])
		},
		mounted() {
			// #ifdef APP-PLUS
			uni.onNetworkStatusChange(function(data) {
				// 根据返回的数据判断网络状态并进行相应操作
				if (data.isConnected) {
					// 网络已连接
					console.log('已连接网络');
					// 在这里可以执行你的逻辑
					store.commit('SET_IS_CONNECTED', {
						isConnected: true
					})
				} else {
					// 网络已断开
					console.log('网络已断开');
					// 在这里可以执行你的逻辑
					store.commit('SET_IS_CONNECTED', {
						isConnected: false
					})
				}
			});
			// #endif
		},
		methods: {
			...mapMutations(['SET_BASIC_INFO', '$uStore']),
			initDatebase() {
				// const database = uniDB.openDatabase({
				// 	name: dbPath
				// })

				// database.executeSql('CREATE TABLE IF NOT EXISTS my_table (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)')
			},
			plusReady() {
				plus.runtime.getProperty(plus.runtime.appid, (inf) => {
					this.wgtVer = inf.version;
					console.log("当前应用版本：" + this.wgtVer);
					this.updateVersion()
				});
			},
			async updateVersion() {
				let data = (await this.getVersionList()).rows[0]
				console.log('当前最新版本：', data.code);
				if (data.code !== this.wgtVer) {
					uni.showModal({ //提醒用户更新
						title: "更新提示",
						content: '版本不相等，需要更新',
						success: (res) => {
							if (res.confirm) {
								if (data.updatePackageType == 0) {
									plus.runtime.openURL(data.appUrl);
								} else if (data.updatePackageType == 1) {
									uni.downloadFile({
										url: data.appUrl,
										success: (downloadResult) => {
											if (downloadResult.statusCode === 200) {
												uni.showLoading({
													title: '加载中',
													mask: true
												});
												plus.runtime.install(downloadResult
													.tempFilePath, {
														force: true
													},
													function() {
														uni.hideLoading();
														plus.runtime.restart();
													},
													function(e) {
														console.error('install fail...');
														uni.hideLoading();
														uni.showModal({
															titile: '提示',
															content: 'error',
															success: (res) => {}
														})
													});
											}
										}
									});
								}
							}
						}
					})
				} else {
					console.log('版本相等，不需要更新');
				}
			},
			autoLogin() {
				uni.getStorage({
					key: 'isLogin',
					complete: (res) => {
						if (res.data.isLogin) {
							uni.reLaunch({
								url: '/pages/sys/home/index'
							});
						}
					}
				});

			},
			getVersionList(params) {
				return new Promise((resolve, reject) => {
					this.$u.api.verison.list({
						pageSize: 1,
						pageNum: 1
					}).then(res => {
						resolve(res)
					})
				})
			},
		}
	}
</script>
<style lang="scss">
	/* #ifndef APP-PLUS-NVUE */
	@import url("~@/static/iconfont/iconfont.css");
	@import "uview-ui/index.scss";
	@import url("~@/styles/common.scss");

	//@import url("~@/styles/common1.scss");

	page {
		background-color: $u-bg-color;
	}
	/* #endif */

	/* #ifndef APP-PLUS-NVUE || MP-WEIXIN */
	@import "./styles/flex.css";
	/* #endif  */


	// .datetime-picker-sepcil.uni-date {
	// 	width: auto !important;

	// 	.uni-date-x--border {
	// 		border: none !important;

	// 		.uni-date-x.uni-date-single {
	// 			flex: inherit;

	// 			.icon-calendar {
	// 				display: none;
	// 			}

	// 			.uni-date__x-input {
	// 				font-size: 16px;
	// 			}
	// 		}
	// 	}
	// }

	.uni-forms-item__content {
		max-width: 80%;
	}
</style>